package com.brillilab.service.core.plans.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.plans.PlanCommentMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.plans.PlanComment;
import com.brillilab.service.core.plans.IPlanCommentService;

/**
 * <p>
 * 计划评论 服务实现类
 * </p>
 *
 * @author zlb
 * @since 2019-09-11
 */
@Service
@Transactional
public class PlanCommentServiceImpl extends ServiceImpl<PlanCommentMapper, PlanComment> implements IPlanCommentService {

	@Resource
	private PlanCommentMapper planCommentMapper;

	@Override
	public PlanComment insert(PlanComment planComment) {
		int insert = planCommentMapper.insert(planComment);
		return insert == 1 ? planComment : null;
	}

	@Override
	public boolean deleteById(Long id) {
		int i = planCommentMapper.deleteById(id);
		return i > 0 ? true : false;
	}

	@Override
	public PlanComment selectById(Long id) {
		return planCommentMapper.selectById(id);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<PlanComment> getPlanCommentList(Long planLabMemberId, String startTime, String endTime) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planLabMemberId);
		queryWrapper.ge(PlanComment::getPlanDate, startTime);
		queryWrapper.le(PlanComment::getPlanDate, endTime);
		queryWrapper.orderByDesc(PlanComment::getCreateTime);
		List<PlanComment> list = planCommentMapper.selectList(queryWrapper);
		return list;
	}

	@Override
	public void updateRead(Long planLabMemberId, String startTime, String endTime) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planLabMemberId);
		queryWrapper.ge(PlanComment::getPlanDate, startTime);
		queryWrapper.le(PlanComment::getPlanDate, endTime);
		PlanComment entity = new PlanComment();
		entity.setIsRead(BoolEnum.TRUE.getValue());
		planCommentMapper.update(entity, queryWrapper);
	}

	@Override
	public int getNoReadNum(Long planMemberId, String startDate, String endDate) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planMemberId);
		queryWrapper.ge(PlanComment::getPlanDate, startDate);
		queryWrapper.le(PlanComment::getPlanDate, endDate);
		queryWrapper.eq(PlanComment::getIsRead, BoolEnum.FALSE.getValue());
		return planCommentMapper.selectCount(queryWrapper);
	}

	@Override
	public int getUnreadNum(Long planMemberId) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planMemberId);
		queryWrapper.eq(PlanComment::getIsRead, BoolEnum.FALSE.getValue());
		return planCommentMapper.selectCount(queryWrapper);
	}

	@Override
	public void updateRead(Long planMemberId) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planMemberId);
		PlanComment entity = new PlanComment();
		entity.setIsRead(BoolEnum.TRUE.getValue());
		planCommentMapper.update(entity, queryWrapper);
	}

	@Override
	@SuppressWarnings("unchecked")
	public PageVo<PlanComment> commentPage(Long planMemberId, Integer pageNum, Integer pageSize) {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PlanComment::getPlanMemberId, planMemberId);
		queryWrapper.orderByDesc(PlanComment::getCreateTime);
		IPage<PlanComment> page = new Page<PlanComment>(pageNum, pageSize);
		IPage<PlanComment> iPage = planCommentMapper.selectPage(page, queryWrapper);
		return new  PageVo<PlanComment>(iPage);
	}

	@Override
	public List<PlanComment> selectAllList() {
		LambdaQueryWrapper<PlanComment> queryWrapper = new LambdaQueryWrapper<>();
		return planCommentMapper.selectList(queryWrapper);
	}
}
